package com.idp.web.test.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.idp.common.persistence.Page;
import com.idp.ebay.domain.EBayInfo;
import com.idp.web.system.entity.SysOrg;
import com.idp.web.test.dao.OrderItemDao;
import com.idp.web.test.dao.OrdertDao;
import com.idp.web.test.dao.PersonDao;
import com.idp.web.test.dao.ProductDao;
import com.idp.web.test.entity.OrderItem;
import com.idp.web.test.entity.Ordert;
import com.idp.web.test.entity.Person;
import com.idp.web.test.entity.Product;
import com.idp.web.test.service.OrdertService;

/**
 * 
 * 123123service实现类
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 *         <pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Service("ordertService")
public class OrdertServiceImpl implements OrdertService {

	@Resource
	private OrdertDao ordertDao;

	@Resource
	private ProductDao productDao;

	@Resource
	private PersonDao personDao;

	@Resource
	private OrderItemDao orderItemDao;

	@Override
	public Page<Ordert> findByPage(Ordert ordert, Page<Ordert> page) {

		page.setResult(ordertDao.find(ordert, page));

		return page;
	}

	@Override
	public List<Ordert> findBySearch(Ordert ordert) {

		return ordertDao.find(ordert);
	}

	@Override
	public Ordert getById(Integer id) {

		return ordertDao.getById(id);
	}

	@Override
	public void add(Ordert ordert) {

		ordertDao.add(ordert);
	}

	@Override
	public void update(Ordert ordert) {

		ordertDao.update(ordert);
	}
	
	@Override
	public void updateNotNull(Ordert ordert) {

		ordertDao.updateNotNull(ordert);
	}

	@Override
	public void delete(Integer id) {

		ordertDao.delete(id);
	}

	@Override
	public Integer synchronization(List<EBayInfo> infos, Integer platformId,Integer accountid) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Integer i = 0;
		Date date = ordertDao.getCreateTimeByPlatformDesc(platformId);
		Date orderDate = null;
		Person person = new Person();
		Product product = new Product();
		Ordert ordert = new Ordert();
		OrderItem orderItem = new OrderItem();
		for (EBayInfo eBayInfo : infos) {
			try {
				System.out.println(eBayInfo.getCreateTime());
				orderDate = simpleDateFormat.parse(eBayInfo.getCreateTime());
				if (date == null || orderDate.after(date)) {

					person.setAddress(eBayInfo.getShipAddress());
					person.setName(eBayInfo.getShipname());
					person.setPhone(eBayInfo.getPhone());
					person.setUid(eBayInfo.getBuyerId());
					person.setZipcode(eBayInfo.getPostalCode());

					product = productDao.getByCode(eBayInfo.getSku());
					if (product == null) {
						product = new Product();
						product.setCode(eBayInfo.getSku());
						product.setName(eBayInfo.getProductTitle());
						product.seteBayCode(eBayInfo.getSku());
						product.setProductStatusId(1);
						productDao.add(product);
					}
					orderItem.setProductId(product.getId());
					orderItem.setSku(eBayInfo.getSku());
					orderItem.setTotalPrice(Double.valueOf(eBayInfo.getTotal()));
					orderItem.setPrice(Double.valueOf(eBayInfo.getSubTotal()));
					orderItem.setCount(Integer.valueOf(eBayInfo.getQuanity()));

					personDao.add(person);

					ordert.setCode(eBayInfo.getOrderId());
					Integer orderstatus = 1;
					if (eBayInfo.getOrderStatus().equals("Completed")) {

						if (eBayInfo.getShiptime().equals("未发货")) {
							orderstatus = 1;// 未发货所以待发货
						} else {
							orderstatus = 2;// 2是已发货
						}

					} else {
						orderstatus = 5;// 5已取消
					}
					ordert.setOrderStatusId(orderstatus);
					ordert.setCreatetime(eBayInfo.getCreateTime());
					ordert.setPlatformId(platformId);
					ordert.setWarehouseId(1);
					ordert.setPersonId(person.getId());
					ordert.setPrice(Double.valueOf(eBayInfo.getTotal()));
					ordert.setCount(Integer.parseInt(eBayInfo.getTransactionLength()));
					System.err.println("order设置值="+accountid);
					ordert.setAccountId(accountid);
					ordertDao.add(ordert);

					orderItem.setOrdertId(ordert.getId());
					System.out.println(orderItem);
					orderItemDao.add(orderItem);
					i++;
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return i;

	}

	@Override
	public Page<Ordert> getByPlatformId(Integer platformId, Page<Ordert> page) {
		page.setResult(ordertDao.getByPlatformId(platformId, page));
		return page;
	}

	@Override
	public List<Ordert> getByDate(String startDate, String endDate) {

		List<Ordert> olist = ordertDao.getByDate(startDate, endDate);
		return olist;
	}

	@Override
	public List<Ordert> getByDateAll(String startDate, String endDate) {

		List<Ordert> olist = ordertDao.getByDateAll(startDate, endDate);
		return olist;
	}

	@Override
	public List<Ordert> getByDateUtil(String startDate, String endDate, Integer platformId) {

		List<Ordert> olist = ordertDao.getByDateUtil(startDate, endDate, String.valueOf(platformId));
		return olist;
	}

	@Override
	public Ordert findByCode(String code) {
		Ordert o = ordertDao.findByCode(code);
		return o;
	}

	@Override
	public List<OrderItem> findItems(int orderid) {
		List<OrderItem> olist = orderItemDao.findByOrderId(orderid);
		return olist;
	}

	@Override
	public List<Ordert> findByStatus(String startDate, String endDate, Integer status) {
		System.out.println("findByStatus," + startDate + "," + endDate + "," + status);
		String statuss = String.valueOf(status);
		List<Ordert> olist = ordertDao.findByStatus(startDate, endDate, statuss);
		return olist;
	}

	@Override
	public List<Ordert> findByStatusUtil(String startDate, String endDate, Integer status, Integer platformId) {
		System.out.println("findByStatus," + startDate + "," + endDate + "," + status);
		String statuss = String.valueOf(status);
		List<Ordert> olist = ordertDao.findByStatusUtil(startDate, endDate, statuss, String.valueOf(platformId));
		return olist;
	}
}